1# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: net_hmac_authe_serv.py 
@time: 2019/7/12 下午2:42
"""
'''
创建客户端与服务端认证服务
'''
from socket import socket, AF_INET, SOCK_STREAM
from net_hmac_authe import server_authenticate
secret_key = b'peekaboo'

def echo_handler(address, client_sock):
    print('Got connection from ', address)
    if not server_authenticate(client_sock, secret_key):
        client_sock.close()
        return
    while True:
        msg = client_sock.recv(8192)
        if not msg:
            break

        client_sock.sendall(msg)

def echo_server(address):
    s = socket(AF_INET, SOCK_STREAM)
    s.bind(address)
    s.listen(5)
    while True:
        c, a = s.accept()
        echo_handler(address,c)

echo_server(('', 18000))


